package view.panel.prompt
{
	import control.panel.prompt.IPromptPanelSelectCombineCardsControl;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import model.card.ICardData;
	
	import utils.DisplayTool;
	
	import view.card.Card;
	import view.card.ICard;

	public class SelectCombine extends Sprite
	{
		private static var border:Shape = new Shape();
		private var promptSelectCombineCardsControl:IPromptPanelSelectCombineCardsControl;
		private var cardDatas:Array; 
		private var cards:Array = [];
		
		public function SelectCombine(promptSelectCombineCardsControl:IPromptPanelSelectCombineCardsControl)
		{
			this.promptSelectCombineCardsControl = promptSelectCombineCardsControl;
			this.mouseChildren = false;
			this.buttonMode = true;
			addListeners();	
			
		}
		
		private function addListeners():void
		{
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
		}
		
		private function showBoarder():void
		{
			this.addChild(border);
			with(border.graphics)
			{
				clear();
				lineStyle(2,0xFFFF00);
				drawRoundRect(0,0,width,height,3,3);
			}
		}
		
		private function hideBoarder():void
		{
			DisplayTool.removeChild(this,border);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			showBoarder();
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			hideBoarder();
		}
		
		private function onMouseDown(event:MouseEvent):void
		{
			event.stopPropagation();
			promptSelectCombineCardsControl.selectCombineCards(cardDatas);
		}
		
		public function showCards(cardDatas:Array):void
		{
			this.cardDatas = cardDatas;
			var card:ICard;
			var cardData:ICardData;
			
			if (cardDatas == null)
			{
				return;
			}
			
			for (var i:int=0; i<cardDatas.length; i++)
			{
				cardData = cardDatas[i];
				card = new Card();
				card.updateData(cardData);
				cards.push(card);
				card.self.x = i * 20;
				card.self.y = 0;
				addChild(card.self);
			}
		}
		
		public function destroy():void
		{
			var card:ICard;
			for (var i:int=0; i<cardDatas.length; i++)
			{
				card = cards[i];
				card.destroy();
			}
			cardDatas.length = 0;
			cards.length = 0;
			cardDatas = null;
			cards = null;
			promptSelectCombineCardsControl = null;
		}
	}
}